package com.example.yixueban.service;

import com.example.yixueban.Tool.SnowFlake;
import com.example.yixueban.domain.Teacher;
import com.example.yixueban.domain.User;
import com.example.yixueban.mapper.AdminMapper;
import com.example.yixueban.mapper.TeacherMapper;
import com.example.yixueban.mapper.UserMapper;
import com.example.yixueban.req.main.Login_Req;
import com.example.yixueban.req.main.Register_Req;
import com.example.yixueban.req.main.TeacherList_Req;
import com.example.yixueban.resp.login.AccountInfo_resp;
import com.example.yixueban.service_interface.MainService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: Wang YiHan
 * Date: 2021/8/4 14:58
 * Email: 783096044@qq.com
 * File_Name: null.java
 **/
@Service
public class MainServiceImpl implements MainService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private SnowFlake snowFlake;

/*登录*/
    @Override
    public AccountInfo_resp login(Login_Req loginInfo) {
        AccountInfo_resp account = new AccountInfo_resp();
        AccountInfo_resp errorResult = new AccountInfo_resp();
       // List<AccountInfo_resp> result = new ArrayList<>();
        boolean loginSuccess=false;
        //检查身份
        int identify = Integer.parseInt(loginInfo.getIdentity());//身份  0 为用户，1为教师
        //检查登录方式：是账号密码型还是手机验证码型
        int loginType = 1; //默认为账号密码登录
        if (!(loginInfo.getTelephone().equals("") &&loginInfo.getCode().equals("")))
            loginType =0;   //如果手机号和二维码未输入，则为账号密码型登录
       // System.out.println("loginType="+loginType+"     identify="+identify);
        //验证登录
        if (loginType==1) {
            String user_number = loginInfo.getUser_number();
            String pwd = loginInfo.getPassword();
            if (identify == 0) {
                //System.out.println("userMapper.userLogin");
                account = userMapper.userLogin(user_number, pwd);
            }else{
               // System.out.println("teacherMapper.teacherLogin");
                account = teacherMapper.teacherLogin(user_number, pwd);
            }
           //返回登录结果
            if (account==null){   //没select到此用户
                errorResult.setTemp(1);
                return errorResult;
            }else{
                account.setTemp(0);
                account.setIdentity(identify);
                return  account;
            }
        }else{
            String telephone = loginInfo.getTelephone();
            String yzm = loginInfo.getCode();
            /*//手机验证码业务。。。。。。
            if (identify == 0)
                account = user YZM;
            else
                account = teacher YZM*

                if验证码错误
                则把AccountInfo_resp.id =0(表示resp.success=false ,用于controller)
                    AccountInfo_resp.head_portrait String类型 刚好可以帮助显示错误信息：验证码错误，供controller使用
                /
             */

        }

        return account;
    }

    @Override
    public AccountInfo_resp register(Register_Req registerInfo) {
        /*思路：先检查注册方式：账号密码还是手机号
        账号密码的话，检查账号或者密码的合法性
        检查是否有同样的账号（确认账号唯一性）
        生成账号*/
        AccountInfo_resp account = new AccountInfo_resp();
        account.setTemp(0); //默认注册成功
        //检查身份
        int identify = Integer.parseInt(registerInfo.getIdentity());//身份  0 为用户，1为教师
        //检查登录方式：是账号密码型还是手机验证码型
        int registerType = 1; //默认为账号密码登录
        if (!(registerInfo.getTelephone().equals("") &&registerInfo.getCode().equals("")))
            registerType =0;   //如果手机号或者二维码不为空，则采取手机号注册

        if (registerType==1){
            String inputNumber = registerInfo.getUser_number();  //用户输入的账号
            System.out.println("输入的账号为 "+inputNumber);
            //检查唯一性
            if (identify == 0) {
                User user = userMapper.selectByUser_number(inputNumber);

                if (user != null) {
                    account.setTemp(1);
                    return account;
                }

                long registerID = snowFlake.nextId();
                System.out.println("如果是雪花创建的新id为："+registerID);
                String user_number =registerInfo.getUser_number();
                String user_password=registerInfo.getPassword();

                int insertFlag =userMapper.userRegister(user_number,user_password);
                if (insertFlag==0)   //如果增加insert失败的话，设置标志位
                    account.setTemp(2);
                return account;
            } else {
                Teacher teacher = teacherMapper.selectByTeacher_number(inputNumber);

                if (teacher != null) {
                    account.setTemp(1);
                    return account;
                }
                long registerID = snowFlake.nextId();
                System.out.println("如果是雪花创建的新id为："+registerID);
                String teacher_number =registerInfo.getUser_number();
                String teacher_password=registerInfo.getPassword();

                int insertFlag =teacherMapper.teacherRegister(teacher_number,teacher_password);
                if (insertFlag==0)   //如果增加insert失败的话，设置标志位
                    account.setTemp(2);
                return account;
            }
        }



        return account;
    }

    @Override
    public List<TeacherList_Req> getTeacherReqList() {
        return teacherMapper.getTeacherReqList();
    }
    /*

    */
/*注册*//*

    @Override
    public List<AccountInfo_resp> register(Register_Req registerInfo) {
        AccountInfo_resp account = new AccountInfo_resp();
        List<AccountInfo_resp> result = new ArrayList<>();

        //检查身份
        int identify = Integer.parseInt(registerInfo.getIdentity());//身份  0 为用户，1为教师
        //检查登录方式：是账号密码型还是手机验证码型
        int registerType = 1; //默认为账号密码登录
        if (registerInfo.getTelephone().equals("") &&registerInfo.getCode().equals(""))
            registerType =0;   //如果手机号和二维码未输入，则为账号密码型登录
        if (registerType==1) {
            String username = registerInfo.getUsername();
            String pwd = registerInfo.getPassword();
            if (identify == 0) {
                //雪花算法生成id
                String id="";
                account = userMapper.userRegister(id,username, pwd);
                account.setIdentity(0);
            }else{
                //account = teacherMapper.teacherRegister(username, pwd);
                account.setIdentity(1);
            }
        }else{
            String telephone = registerInfo.getTelephone();
            String yzm = registerInfo.getCode();
            */
/*//*
/手机验证码业务。。。。。。
            如果验证码正确，则检查是user还是teacher，否则输出错误信息；
            得知身份后，则注册，insert入id和手机号
            if (identify == 0){
                account = user YZM;
            else
                account = teacher YZM*//*

        }
        result.add(account);
        return result;


    }
*/


}
